Public Class utilDsn
    Private Function Check_DSN(ByVal pvDSN As String) As Boolean
        Dim reg As RegistryKey = Registry.LocalMachine
        Dim regkey As RegistryKey
        Dim conRegKey1 As String = "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\"
        Dim tmpdsnvalue As String

        Try
            regkey = reg.OpenSubKey(conRegKey1)
            If regkey.ValueCount > 0 Then
                tmpdsnvalue = regkey.GetValue(pvDSN)
                If tmpdsnvalue = "" Then
                    Return False
                Else
                    lblMsg.Text += "The ODBC name " & Chr(34) & pvDSN & Chr(34) & " already exists." & vbCrLf & vbCrLf
                    Return True
                End If
            Else
                Return False
            End If
        Catch err As Exception
            lblMsg.Text += MsgBox(err.Message)
        End Try
    End Function

    Private Sub Create_DSN(ByVal pvDSN)
        Dim reg As RegistryKey = Registry.LocalMachine
        Dim regkey As RegistryKey
        Dim conRegKey1 As String = "SOFTWARE\ODBC\ODBC.INI\" & pvDSN
        Dim conRegKey2 As String = "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources"
        Dim DriverPath As String = GetFolderPath(SpecialFolder.System) & "\sqlsrv32.dll"

        'Dim permission As System.Security.Permissions.RegistryPermission = New _
        ' System.Security.Permissions.RegistryPermission(Security.Permissions.PermissionState.Unrestricted)

        Try
            regkey = reg.CreateSubKey(conRegKey1)
            regkey.SetValue("Database", "O")
            regkey.SetValue("Description", " O Database")
            regkey.SetValue("Driver", DriverPath)
            regkey.SetValue("LastUser", "h")
            regkey.SetValue("Server", "O")
            regkey.Close()
            reg.Close()

            regkey = reg.CreateSubKey(conRegKey2)
            regkey.SetValue(pvDSN, "SQL Server")
            regkey.Close()
            reg.Close()

            lblMsg.Text += "Successfully created ODBC name " & Chr(34) & pvDSN & Chr(34) & "." & vbCrLf & vbCrLf
        Catch err As Exception
            lblMsg.Text += MsgBox(err.Message)
        End Try

    End Sub

    Public Function Create_Shortcut() As Boolean
        Dim SourcePath As String = "\\O\O\O.lnk"
        Dim DestPath As String = GetFolderPath(SpecialFolder.DesktopDirectory) & "\O.lnk"

        Try
            If Dir(SourcePath) <> "" Then
                If Dir(DestPath) = "" Then
                    File.Copy(SourcePath, DestPath)
                    lblMsg.Text += "Successfully created O shortcut on the desktop." & vbCrLf & vbCrLf
                Else
                    lblMsg.Text += "Shortcut already exists." & vbCrLf & vbCrLf
                End If
            Else
                lblMsg.Text += "Shortcut cannot be created. Contact tech support." & vbCrLf & vbCrLf
            End If
        Catch err As Exception
            lblMsg.Text += MsgBox(err.Message)
End Class
